package edu.wy.controller;


import edu.wy.common.result.Result;
import edu.wy.entity.Login;
import edu.wy.service.UserService;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author jianhong_ran
 */
@RestController
@Slf4j
@RequestMapping("/login")
public class LoginController {

    @Resource
    private UserService userService;

    /**
     * 登录 获取验证码
     * @param phone 手机号
     * @param session session
     * @return Result
     */
    @ApiResponse(description = "发送验证码")
    @PostMapping("code")
    public Result sendCode(@RequestParam("phone") String phone, HttpSession session) {
        //发送短信验证码并保存验证码
        return userService.sendCode(phone, session);
    }

    /**
     * 登录功能 账号密码
     * @param login 存储着登录时的账号密码
     * @param session session
     */
    @ApiResponse(description = "登录功能——账号密码")
    @GetMapping("/login")
    public Result login(@RequestBody Login login, HttpSession session){
        //实现登录功能
        return userService.login(login, session);
    }

    /**
     * 登录功能 手机号验证码
     */
    @ApiResponse(description = "登录功能——手机号验证码")
    @GetMapping("/login/phone")
    public Result loginByPhone(@RequestBody Login login, HttpSession session) {
        //实现登录功能
        return userService.loginByPhone(login, session);
    }

    /**
     * 登出功能
     * @param token 得到存储在Redis中的token
     */
    @ApiResponse(description = "登出功能")
    @PostMapping("/logout")
    public Result loginOut(@RequestParam String token) {
        return userService.loginOut(token);
    }


}
